﻿using System;

namespace Program17
{
    /// <summary>
    /// 泛型委托
    /// </summary>
    public delegate int DelegateCompare<T>(T n1, T n2);
    public class Program
    {
        static void Main(string[] args)
        {
            string[] names = { "asdf", "bbbbbb", "xxxxxxxxxx" };
            string namesMax = GetMax<string>(names, Compare2);
            Console.WriteLine(namesMax);


            int[] nums = { 1, 2, 3, 4, 5 };
            int numsMax = GetMax<int>(nums, Compare1);
            Console.WriteLine(numsMax);
        }
        /// <summary>
        /// 获取数组中最大的元素，int就是最大，string就是最长的
        /// </summary>
        public static T GetMax<T>(T[] item, DelegateCompare<T> del)
        {
            T max = item[0];
            for (var i = 0; i < item.Length; i++)
            {
                if(del(max,item[i])<0)
                {
                    max = item[i];
                }
            }
            return max;
        }
        //整数的比较方法
        public static int Compare1(int n1, int n2)
        {
            return n1 - n2;
        }

        //字符串长度的比较方法
        public static int Compare2(string n1, string n2)
        {
            return n1.Length - n2.Length;
        }
    }
}
